#include "S3C2416.h"

#include "kern/types.h"
#include "kern/kern.h"

#include "stdio.h"

int tickISR(uint32_t interrupt, void *arg)
{
    printf("[%s:%d] TickISR\r\n", __FILE__, __LINE__);
}

void rtcTimeTickSet(void)
{
    RTCCON |= (01 << 0);    
    RTCCON |= (1<<3);    
    RTCCON &= ~(1<<3);
    RTCCON &= ~(1<<4);    
    RTCCON = (RTCCON & ~(0x0F << 5)) | (4 << 5);

    BCDSEC = 0x01;
    BCDMIN = 0x01;
    BCDHOUR = 0x01;
    BCDDATE = 0x01;
    BCDDAY = 0x01;
    BCDMON = 0x01;
    BCDYEAR = 0x01;

    TICNT0 = 0x02;
//    TICNT1 = 0x0F;
//    TICNT2 = 0x00;
    TICNT0 |= (1<<7);
    printf("[%s:%d] rtcTimeTickSet %d\r\n", __FILE__, __LINE__, TICNT0);

    RTCALM = 0xFF;
    ALMSEC = 0x20;
    ALMSEC = 0x01;
    ALMMIN = 0x01;
    ALMHOUR = 0x01;
    ALMDATE = 0x01;
    ALMMON = 0x01;
    ALMYEAR = 0x01;    
    
    RTCCON &= ~(0x1 << 0);
    msleep(1000);
    
    printf("[%s:%d] rtcTimeTickSet %d %d\r\n", __FILE__, __LINE__, TICKCNT, BCDSEC);
    msleep(1000);
    printf("[%s:%d] rtcTimeTickSet %d %d\r\n", __FILE__, __LINE__, TICKCNT, BCDSEC);    
    msleep(1000);
    printf("[%s:%d] rtcTimeTickSet %d %d\r\n", __FILE__, __LINE__, TICKCNT, BCDSEC); 
    msleep(1000);
    printf("[%s:%d] rtcTimeTickSet %d %d\r\n", __FILE__, __LINE__, TICKCNT, BCDSEC); 
    msleep(1000);
    printf("[%s:%d] rtcTimeTickSet %d %d\r\n", __FILE__, __LINE__, TICKCNT, BCDSEC); 
    msleep(1000);
    printf("[%s:%d] rtcTimeTickSet %d %d\r\n", __FILE__, __LINE__, TICKCNT, BCDSEC); 
    msleep(1000);
    printf("[%s:%d] rtcTimeTickSet %d %d\r\n", __FILE__, __LINE__, TICKCNT, BCDSEC); 
}

void enterStopMode(void)
{
    rtcTimeTickSet();
    requestIRQ(INTRTC, tickISR, NULL, NULL);
    INTMSK1 &= ~(1 << INTRTC);

//	OSCSET=0xFFFF;
//	PWRCFG=(0<<17)|(1<<16)|(1<<15)|(0<<9)|(1<<8)|(1<<7)|(0<<4)|(0<<3)|(0<<2)|(1<<0);	
//	PWRMODE=(1<<16);
}
